package edu.uwm.RobberKnight.View;

import java.awt.Color;
import java.awt.Graphics;
import java.util.HashMap;
import java.util.LinkedList;

import javax.swing.JPanel;

import edu.uwm.RobberKnight.Controller.RobberKnightsController;
import edu.uwm.RobberKnight.Model.Player;

public class MultiHandsView extends JPanel {

	private static final long serialVersionUID = -9037609088750100008L;
	private RobberKnightsController _controller;
	private HashMap<Color, LinkedList<GuiTile>> _playerTiles;
	
	public MultiHandsView(RobberKnightsController controller) {
		_controller = controller;
		_playerTiles = new HashMap<Color, LinkedList<GuiTile>>();
		_playerTiles.put(Color.RED, new LinkedList<GuiTile>());
		_playerTiles.put(Color.BLUE, new LinkedList<GuiTile>());
		_playerTiles.put(Color.GREEN, new LinkedList<GuiTile>());
		_playerTiles.put(Color.YELLOW, new LinkedList<GuiTile>());
	}
	
	@Override
	public void paint(Graphics g) {
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, getWidth(), getHeight());
		if(_controller != null) {
			LinkedList<Player> players = _controller.getPlayers();
			if(players != null) {
				int lastPlayerX = 0;
				for(int i = 0; i < players.size(); i++) {
					Player player = players.get(i);
					LinkedList<GuiTile> tileList = _playerTiles.get(player.getColor());
					for(int j = 0; j < tileList.size(); j++) {
						GuiTile tile = tileList.get(j);
						tile.setX(5 + (GuiTile.TILE_SIZE / 2 * j) + (5 * j) + lastPlayerX);
						if(i == 0 || i == 1)
							tile.setY(10);
						else
							tile.setY(16 + (GuiTile.TILE_SIZE / 2));
						tile.drawTileBack(g, .5f);
						if(j == tileList.size() - 1)
						{
							if(i == 1)
								lastPlayerX = 0;
							else
								lastPlayerX = tile.getX() + (GuiTile.TILE_SIZE / 2) + 5;
						}
					}
				}
			}
		}
	}
	
	public void updateState(long time) {
		if(_controller.getPlayers() != null) {
			for(Player player : _controller.getPlayers()) {
				HandsView.updateTileCollection(player, _playerTiles.get(player.getColor()));
			}
		}
	}
}
